package com.yz.core.current;

import java.util.concurrent.locks.StampedLock;

public class StampedSample {
    private final StampedLock sl =new StampedLock();
    void mutate() {
        long samp = sl.writeLock();
        try {
            write();
        } finally {
            sl.unlockWrite(samp);
        }
    }

    private void write() {
    }

    Data access() {
        long samp = sl.tryOptimisticRead();
        Data data = read();
        if (!sl.validate(samp)) {
            samp = sl.readLock();
            try {
                data = read();
            } finally {
                sl.unlockRead(samp);
            }
        }
        return data;
    }


    private Data read() {
        return new Data();
    }

    // …
    class Data{

    }
}